const  { pinyin }= require('pinyin-pro');
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}




/**
 * 将字符串数组按拼音首字母分类、排序
 * @param {string[]} arr - 输入的字符串数组
 * @returns {Array<{letter: string, string[]}>} - 分组后的结果
 */
export function groupByFirstLetter(arr) {
    // Step 1: 过滤无效项并去重
    const filteredArr = [...new Set(arr.filter(item => typeof item === 'string' && item.trim()))];

    // Step 2: 按拼音首字母分组
    const groups = {};
    filteredArr.forEach(item => {
        // 获取拼音首字母（大写）
        const firstChar = pinyin(item.charAt(0), { pattern: 'first', toneType: 'none' }).toUpperCase();
        const letter = /^[A-Z]$/.test(firstChar) ? firstChar : '#';

        if (!groups[letter]) {
            groups[letter] = [];
        }
        groups[letter].push(item);
    });

    // Step 3: 对字母组和组内数据排序
    const sortedLetters = Object.keys(groups).sort((a, b) => {
        if (a === '#') return 1;
        if (b === '#') return -1;
        return a.localeCompare(b);
    });

    let sorted=sortedLetters.sort((a, b) => a.localeCompare(b, 'zh-Hans-CN'))
    return sorted.map(item=>{
        return{
			"letter": item,
            "data":groups[item],
        }
        
    })

}

//定时获取时间
export function	getNowTime(){
		
		const now = new Date();
		const year = now.getFullYear();
		const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，并补零
		const day = String(now.getDate()).padStart(2, '0');
		const hours = String(now.getHours()).padStart(2, '0');
		const minutes = String(now.getMinutes()).padStart(2, '0');
		const seconds = String(now.getSeconds()).padStart(2, '0');
		const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
		return formattedDateTime
		
}
